//
//  Company_DetailPersonListViewController.m
//  HuaRun2
//
//  Created by admin on 17/9/4.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "Company_DetailPersonListViewController.h"
#import "ContactTableViewCell.h"
#import "TwoLabelView.h"
#import "Meeting_Detail_OutPerson.h"
#import "Contact_DetailViewController.h"
#import "ContactPersonCell.h"
#import "ContactPersonModel.h"

@interface Company_DetailPersonListViewController ()

@end

@implementation Company_DetailPersonListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tableView.backgroundColor = RGBA(249, 249, 249, 1);
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;

    if (self.personListType == PersonListTypeCompanyDet) {
        self.navigationItem.title = NSLocalizedString(@"company_detail_contacts", nil);
//        [NSString stringWithFormat:@"%@-%@",
//                                     NSLocalizedString(@"公司详情",nil),
//                                     NSLocalizedString(@"联系人", nil)];
    }else if (self.personListType == PersonListTypeYuHui) {
        //    "meetings_member_out"= "外部与会人员";//
        self.navigationItem.title = NSLocalizedString(@"meetings_member_out", nil);
//        @"外部与会人员";
    }
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[ContactTableViewCell class] forCellReuseIdentifier:@"ContactTableViewCell"];
    [self.tableView registerClass:[ContactPersonCell class] forCellReuseIdentifier:@"ContactPersonCell"];


    TwoLabelView * headerView = [TwoLabelView new];
    headerView.frame = CGRectMake(kMargin10, 0, kMainScreenWidth - 20, 60);
    headerView.nameLab.text = [NSString showSpaceStringWithStr:_name];
    headerView.companyLab.text = [NSString showSpaceStringWithStr: _companyName];
    self.tableView.tableHeaderView = headerView;

    [self dataInit];
    [self methodBind];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)methodBind {
    weakself(ws);
    if (self.personListType == PersonListTypeYuHui) {
    
        [self.tableView headerRefresh:^{
            [ws refreshData];
        }];
    }

}
- (void)dataInit {
    self.current = 1;
    self.size = 10;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_personListType == PersonListTypeCompanyDet) {
        ContactPersonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContactPersonCell"
                                                                     forIndexPath:indexPath];
        
        cell.contactPersonModel = _dataArray[indexPath.row];
        return cell;
    }

    ContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContactTableViewCell"
                                                                 forIndexPath:indexPath];
    cell.meeting_Detail_OutPerson = _dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_personListType == PersonListTypeCompanyDet) {
        return 80;
    }

    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (_personListType == PersonListTypeYuHui) {

        Contact_DetailViewController * meetingVC = [[Contact_DetailViewController alloc] init];
        meetingVC.hidesBottomBarWhenPushed = YES;
        Meeting_Detail_OutPerson * meeting_Detail_OutPerson = _dataArray[indexPath.row];
        meetingVC.contactId = meeting_Detail_OutPerson.contactCode;
        [self.navigationController pushViewController:meetingVC animated:YES];

    }else if (_personListType == PersonListTypeCompanyDet) {
        Contact_DetailViewController * meetingVC = [[Contact_DetailViewController alloc] init];
        meetingVC.hidesBottomBarWhenPushed = YES;
        ContactPersonModel * personModel = self.dataArray[indexPath.row];
        meetingVC.contactId = personModel.id;
        [self.navigationController pushViewController:meetingVC animated:YES];

    }
    
}
    
#pragma mark method
//获取外部与会人员个数
- (void)fetchOutContactPersonNum {
    
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl, kMeeting_Detail_Out_PersonListUrl];
    NSDictionary * dic = @{@"meetingId": _meetingId,
                           @"current": @(_current),
                           @"size": @(_size)};
    
    weakself(ws);
    [NetWorkHelper getWithURLString:url parameters:dic success:^(id object) {
        [ws.tableView endHeaderRefresh];

        if (![object[@"code"] isEqual: kSuccessCode]) {
            NSString * info = [NSString isemptyStr:object[@"msg"]] ? kUnknownError : object[@"msg"];
            [ws showWithText:info];
            return;
        }
        
        NSArray * addressArray = [NSArray yy_modelArrayWithClass:[Meeting_Detail_OutPerson class]
                                                            json:object[@"list"]];
        
        ws.dataArray = ws.current == 1 ? addressArray : [ws.dataArray arrayByAddingObjectsFromArray:addressArray];
        [ws.tableView reloadData];
        
        if (addressArray.count >= ws.size) {
            [ws.tableView footerRefresh:^{
                [ws loadMoreData];
            }];
        }else {
            [ws.tableView hiddenFooter];
        }

    } failure:^(NSError *error) {
        
        [ws.tableView endHeaderRefresh];
        [ws showWithText:kNetworkError];
    }];
    
}
- (void)refreshData {
    self.current = 1;
    [self fetchOutContactPersonNum];
}

- (void)loadMoreData {
    self.current++;
    [self fetchOutContactPersonNum];
}


@end
